package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.saas.domain.dao.CommunicatePlanUserDao;
import com.xbongbong.saas.domain.entity.CommunicatePlanUserEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.model.CommunicatePlanUserModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author long.rao
 * @version v1.0
 * @date 2019/2/1 11:02
 * @since v1.0
 */
@Service("communicatePlanUserModel")
public class CommunicatePlanUserModelImpl implements CommunicatePlanUserModel {
    @Resource
    private CommunicatePlanUserDao communicatePlanUserDao;
    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;

    @Override
    public Integer insert(CommunicatePlanUserEntity entity) throws XbbException {
        Integer insert = communicatePlanUserDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = transferSubFormHelper.transferUserTeamToFormData(IndexTypeEnum.IDX_SAAS_COMMUNICATE_PLAN_TEAM, entity);
        InsertDTO saveDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_COMMUNICATE_PLAN_TEAM);
        paasEsModel.insert(saveDTO);
        return insert;
    }

    @Override
    public Integer update(CommunicatePlanUserEntity entity) throws XbbException {
        Integer update = communicatePlanUserDao.update(entity);
        PaasFormDataEntity paasFormDataEntity = transferSubFormHelper.transferUserTeamToFormData(IndexTypeEnum.IDX_SAAS_COMMUNICATE_PLAN_TEAM, entity);
        InsertDTO saveDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_COMMUNICATE_PLAN_TEAM);
        paasEsModel.update(saveDTO);
        return update;
    }

    @Override
    public List<CommunicatePlanUserEntity> findEntitys(Map<String, Object> param) {
        return communicatePlanUserDao.findEntitys(param);
    }

    @Override
    public List<UserTeamEntity> findBaseEntitys(Map<String, Object> map){
        return communicatePlanUserDao.findBaseEntitys(map);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return communicatePlanUserDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public List<Object> getPlanList(String corpid, List<String> userIdList) {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        if (!userIdList.isEmpty()){
            map.put("userIdList",userIdList);
        }
        return communicatePlanUserDao.getPlanList(map);
    }

    @Override
    public List<Long> getDataIdList(String corpid, List<String> userId) {
        return communicatePlanUserDao.getDataIdList(corpid,userId);
    }


    @Override
    public Integer dataConsistencyUpdateUser(String corpid, String userId, String userName, String userAvatar) {
        return communicatePlanUserDao.dataConsistencyUpdateUser(corpid, userId, userName, userAvatar);
    }

    @Override
    public void insertBatch(List<CommunicatePlanUserEntity> communicatePlanUserEntityList) throws XbbException {
        if (CollectionsUtil.isEmpty(communicatePlanUserEntityList)) {
            return;
        }
        communicatePlanUserDao.insertBatch(communicatePlanUserEntityList);
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_COMMUNICATE_PLAN_TEAM, communicatePlanUserEntityList);

        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            insertDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_COMMUNICATE_PLAN_TEAM));
        }

        paasEsModel.insertBatch(insertDTOList);
    }
}
